package view;

import java.util.ArrayList;
import java.util.List;

import oracle.adf.model.BindingContext;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.binding.BindingContainer;

import oracle.jbo.Key;
import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;

public class TestPageBean
{
  private List selectedItems;

  public TestPageBean()
  {
  }
  
  public BindingContainer getBindings()
  {
    return BindingContext.getCurrent().getCurrentBindingsEntry();
  }

  public void setSelectedItems(List selectedItems)
  {
    List oldSelectedItems = this.selectedItems;
    this.selectedItems = selectedItems;
    
    BindingContainer bindings = getBindings();
    DCIteratorBinding iterBind= (DCIteratorBinding)bindings.get("AllEmpJobPairsView2Iterator");
    
    for ( int newIndex = 0; newIndex < ( this.selectedItems == null ? 0 : this.selectedItems.size() ); newIndex++ )
    {
      // traverse through all newly selected items and update model only for those items that 
      // were not selected earlier and have been recently selected.
      if ( oldSelectedItems == null || !oldSelectedItems.contains( this.selectedItems.get(newIndex) ))
      {
        RowSetIterator rows = iterBind.getRowSetIterator();
        Row[] newlyAddedRows = rows.findByKey( new Key( new Object[]{null, this.selectedItems.get(newIndex)} ), 1 );        
        if ( newlyAddedRows != null && newlyAddedRows.length == 1 )
        {
          newlyAddedRows[0].setAttribute( "AreAssociated", true );
        }
      }
    }
    
    for ( int oldIndex = 0; oldIndex < ( oldSelectedItems == null ? 0 : oldSelectedItems.size() ); oldIndex++ )
    {
      // traverse through all old items and update model only for those items that 
      // were selected earlier and have been recently removed from selected.
      if ( this.selectedItems == null || !this.selectedItems.contains( oldSelectedItems.get(oldIndex) ))
      {
        RowSetIterator rows = iterBind.getRowSetIterator();
        Row[] justRemovedRows = rows.findByKey( new Key( new Object[]{null, oldSelectedItems.get(oldIndex)} ), 1 );        
        if ( justRemovedRows != null && justRemovedRows.length == 1 )
        {
          justRemovedRows[0].setAttribute( "AreAssociated", false );
        }
      }
    }
  }

  public List getSelectedItems()
  {
    if ( selectedItems == null )
    {
      selectedItems = new ArrayList();
    }
    else
    {
      selectedItems.clear();
    }
    
    // get all selected items based on the master-detail association available in the intersection table.
    BindingContainer bindings = getBindings();
    DCIteratorBinding iterBind = (DCIteratorBinding)bindings.get("AllEmpJobPairsView2Iterator");
    Row[] rows = iterBind.getAllRowsInRange();
    for ( int index = 0; index < ( rows == null ? 0 : rows.length ); index++ )
    {
      if ( (Boolean)rows[index].getAttribute("AreAssociated") )
      {
        selectedItems.add( rows[index].getAttribute("JobId") );
      }
    }
    return selectedItems;
  }
}
